package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup> implements SpecGroupService {

    @Autowired
    private SpecParamService paramService;

    //根据分类id查询规格组集合
    @Override
    public List<SpecGroupDTO> queryGroupByCategoryId(Long categoryId) {
        List<SpecGroup> specGroupList = this.query().eq("category_id", categoryId).list();
        //转DTO
        List<SpecGroupDTO> specGroupDTOList = SpecGroupDTO.convertEntityList(specGroupList);
        return specGroupDTOList;
    }

    //根据分类id查询规格组及规格参数
    @Override
    public List<SpecGroupDTO> querySpecList(Long categoryId) {
        //1.查询规格组表
        List<SpecGroupDTO> specGroupDTOList = queryGroupByCategoryId(categoryId);
        if (CollectionUtils.isEmpty(specGroupDTOList)){
            throw new LyException(404,"商品规格组不存在");
        }

        /*//设置属性
        for (SpecGroupDTO specGroupDTO : specGroupDTOList) {
            Long groupId = specGroupDTO.getId();
            //根据group_id查询规格参数表
            List<SpecParamDTO> specParamDTOList = paramService.querySpecParams(null, groupId, null);
            specGroupDTO.setParams(specParamDTOList);
        }*/

        //优化查询逻辑，再for循环里查询数据库多次不合适，放在循环外比较合适

        //2.根据分类id查询分类下的所有规格参数
        List<SpecParamDTO> specParamDTOList = paramService.querySpecParams(categoryId, null, null);
        //3.对这些规格参数根据groupId进行分组，groupId一致的放到一组，形成一个Map(Map的key是groupId，值是groupId一致的所有的param的集合)

        //方式一：普通方式
        /*Map<Long,List<SpecParamDTO>> map = new HashMap<>();

        for (SpecParamDTO specParamDTO : specParamDTOList) {
            Long groupId = specParamDTO.getGroupId();
            //判断map中是否已经存在该分组
            if (!map.containsKey(groupId)){
                //不存在，创建新list
                map.put(groupId,new ArrayList<>());
                map.get(groupId).add(specParamDTO);
            }else {
                //存在，直接存入list
                map.get(groupId).add(specParamDTO);
            }
        }*/

        //方式二  流
        Map<Long, List<SpecParamDTO>> map = specParamDTOList.stream().collect(Collectors.groupingBy(specParamDTO -> specParamDTO.getGroupId()));

        //设置规格组中的List<SpecParamDTO> params属性
        for (SpecGroupDTO specGroupDTO : specGroupDTOList) {
            Long groupId = specGroupDTO.getId();
            //从map中获取params
            List<SpecParamDTO> paramDTOList = map.get(groupId);
            //设置规格组中的List<SpecParamDTO> params属性
            specGroupDTO.setParams(paramDTOList);
        }
        return specGroupDTOList;
    }

    //新增规格组
    @Override
    public void saveGroup(SpecGroupDTO specGroupDTO) {
        SpecGroup specGroup = specGroupDTO.toEntity(SpecGroup.class);
        boolean success = this.save(specGroup);
        if (!success){
            throw new LyException(500,"新增规格组失败！");
        }
    }

    //修改规格组
    @Override
    public void updateGroup(SpecGroupDTO specGroupDTO) {
        SpecGroup specGroup = specGroupDTO.toEntity(SpecGroup.class);
        boolean success = this.updateById(specGroup);
        if (!success){
            throw new LyException(500,"修改规格组失败！");
        }
    }
}